package com.icebartech.core.utils;

import com.alibaba.fastjson.JSONObject;
import java.lang.ref.WeakReference;
import java.util.Collection;
import java.util.List;

import org.dozer.DozerBeanMapper;

import com.google.common.collect.Lists;
import org.dozer.loader.api.BeanMappingBuilder;
import org.dozer.loader.api.TypeMappingOption;

import static org.dozer.loader.api.TypeMappingOptions.mapNull;

/**
 * 简单封装Dozer, 实现深度转换Bean<->Bean的Mapper.实现:
 *
 * 1. 持有Mapper的单例.
 * 2. 返回值类型转换.
 * 3. 批量转换Collection中的所有对象.
 * 4. 区分创建新的B对象与将对象A值复制到已存在的B对象两种函数.
 *
 * @author calvin
 */
public class BeanMapper {
    
    /**
     * 持有Dozer单例, 避免重复创建DozerMapper消耗资源.
     */
    private static DozerBeanMapper dozer = new DozerBeanMapper();
    
    /**
     * 基于Dozer转换对象的类型.
     */
    public static <T> T map(Object source, Class<T> destinationClass) {
        if (source == null) {
            return null;
        }
        return dozer.map(source, destinationClass);
    }
    
    /**
     * 基于Dozer转换Collection中对象的类型.
     */
    public static <T> List<T> mapList(Collection<?> sourceList, Class<T> destinationClass) {
        List<T> destinationList = Lists.newArrayList();
        if (sourceList == null) {
            return destinationList;
        }
        for (Object source : sourceList) {
            T destinationObject = dozer.map(source, destinationClass);
            destinationList.add(destinationObject);
        }
        return destinationList;
    }
    
    /**
     * 全属性复制
     * @param source 源对象
     * @param destinationObject 输出对象
     */
    public static void copy(final Object source, final Object destinationObject) {
        if (null == source || null == destinationObject) return;
        dozer.map(source, destinationObject);
    }
    
    /**
     * 忽略null值的属性复制
     * @param source 源对象
     * @param destinationObject 输出对象
     */
    public static void copyProperties(final Object source, final Object destinationObject) {
        // 自定义配置是全局DozerBeanMapper有效的 并且设定配置必须在map()前 所以这里不使用单例的dozer
        DozerBeanMapper dozer = new DozerBeanMapper();
        dozer.addMapping(new BeanMappingBuilder() {
            @Override
            protected void configure() {
                mapping(source.getClass(), destinationObject.getClass(), mappingBuilder -> {
                    mappingBuilder.mapNull(false);
                    // mappingBuilder.mapEmptyString(false);
                });
            }
        });
        dozer.map(source, destinationObject);
        dozer.destroy();
    }
    
    /**
     * 自定义配置复制
     * @param source 源对象
     * @param destinationObject 输出对象
     */
    public static void copyProperties(final Object source, final Object destinationObject, TypeMappingOption... typeMappingOption) {
        // 自定义配置是全局DozerBeanMapper有效的 并且设定配置必须在map()前 所以这里不使用单例的dozer
        DozerBeanMapper dozer = new DozerBeanMapper();
        dozer.addMapping(new BeanMappingBuilder() {
            @Override
            protected void configure() {
                mapping(source.getClass(), destinationObject.getClass(), typeMappingOption);
            }
        });
        dozer.map(source, destinationObject);
        dozer.destroy();
    }
    
    public static void main(String[] args) {
        Test testA = new Test(null, "value");
        Test testB = new Test(0L, null);
        Test testC = new Test(0L, "value");
        
        System.out.println(JSONObject.toJSONString(testA));
        System.out.println(JSONObject.toJSONString(testB));
        System.out.println(JSONObject.toJSONString(testC));
    
        BeanMapper.copy(testA, testC);
        System.out.println(JSONObject.toJSONString(testA));
        System.out.println(JSONObject.toJSONString(testC));
        
        BeanMapper.copyProperties(testA, testB);
        System.out.println(JSONObject.toJSONString(testA));
        System.out.println(JSONObject.toJSONString(testB));
        
        BeanMapper.copy(testA, testB);
        System.out.println(JSONObject.toJSONString(testA));
        System.out.println(JSONObject.toJSONString(testB));
        
    }
    
    public static class Test {
        Long key;
        String value;
        
        public Test() {
        }
        
        Test(Long key, String value) {
            this.key = key;
            this.value = value;
        }
        
        public Long getKey() {
            return key;
        }
        
        public void setKey(Long key) {
            this.key = key;
        }
        
        public String getValue() {
            return value;
        }
        
        public void setValue(String value) {
            this.value = value;
        }
    }
    
}
